package com.xf.system.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.util.Optional;

/**
 * @Author xiefengf
 * 启动输出地址
 */
public class BaseWebBootstraper {
    private static final Logger log = LoggerFactory.getLogger(BaseWebBootstraper.class);

    public BaseWebBootstraper() {
    }

    public static void run(Class<?> primarySource, String... args){
        try {
            ConfigurableApplicationContext application = SpringApplication.run(primarySource, args);
            Environment env = application.getEnvironment();
            String ip = InetAddress.getLocalHost().getHostAddress();
            String port = ":" + Optional.ofNullable(env.getProperty("server.port")).orElse("8080");
            String path = Optional.ofNullable(env.getProperty("server.servlet.context-path")).filter((p) -> {
                return !"/".equals(p);
            }).orElse("");
            String serviceName = env.getProperty("spring.application.name");
            String active = Optional.ofNullable(env.getProperty("spring.profiles.active")).orElse("default");
            String maxFileSize = Optional.ofNullable(env.getProperty("spring.servlet.multipart.max-file-size")).orElse("1MB");
            String maxRequestSize = Optional.ofNullable(env.getProperty("spring.servlet.multipart.max-request-size")).orElse("10MB");
            String loggingFileMaxSize = env.getProperty("logging.file.max-size");
            String loggingFileMaxHistory = env.getProperty("logging.file.max-history");
            log.info("\n----------------------------------------------------------\n\tService:" + serviceName + " is running! Access URLs:\n\tspring-profiles-active: \t" + active + "\n\tmax-file-size: \t" + maxFileSize + "\n\tmax-request-size: \t" + maxRequestSize + "\n\tlogging.file.max-size: \t" + loggingFileMaxSize + "\n\tlogging.file.max-history: \t" + loggingFileMaxHistory + "\n\tLocal: \t\thttp://localhost" + port + path + "/\n\tExternal: \thttp://" + ip + port + path + "/\n\tSwagger-ui: http://" + ip + port + path + "/swagger-ui/index.html\n\tDoc-ui: \thttp://" + ip + port + path + "/doc.html\n----------------------------------------------------------");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}